package main.剑指OfferⅠ;

import java.util.HashSet;
import java.util.Set;

public class Day10 {
    public static void main(String[] args) {
        System.out.println("剑指Offer46.把数字翻译成字符串");
        System.out.println("剑指Offer48.最长不含重复字符的子字符串");
    }
}

class Offer46{
    public int process(char[] num,int i){
        if (i>=num.length) return 1;
        int counts=0;
        if (i+1<num.length&&(num[i]=='1'||num[i]=='2')){//可扩展至下一位
            int extend=0,noExtend=0;
            if (num[i]=='1'){//可任意扩展
                extend=process(num,i+2);
                noExtend=process(num,i+1);
                counts=extend+noExtend;
            }else if (num[i]=='2'&&num[i+1]<'6'){//当下一位小于6时可扩展
                extend=process(num,i+2);
                noExtend=process(num,i+1);
                counts=extend+noExtend;
            }else {//不可扩展
                counts=process(num,i+1);
            }
        }else counts=process(num,i+1);
        return counts;
    }
    public int translateNum(int num) {
        char[] str=String.valueOf(num).toCharArray();
        int result=process(str,0);
        return result;
    }
}

class Offer48{
    public int lengthOfLongestSubstring(String s) {//滑动窗口更高效
        char[] str=s.toCharArray();
        Set<Character> set=new HashSet<>();
        int max=0;
        for (int i=0;i<str.length;i++){
            for (int j=i;j<str.length;j++){
                if (set.isEmpty()||!set.contains(str[j])) set.add(str[j]);
                else break;
            }
            max=Math.max(max,set.size());
            set.clear();
        }
        return max;
    }
}